
$(document).ready(function () { 
  //记录当前分类的编号id
  let categoryId = -1;
  //记录搜索后的文本
  let serach_text_over = "";


 

  

  //搜索按钮
  $("#seach_btn").click(function(){
    
    let cartgory_Id = $("#btn_text").val();
    let serach_text = $("#search_text").val();
    serach_text_over = serach_text;
    let url = "goods/pages?pageSize=12&pageNum=1&bookname="+serach_text;
    if(cartgory_Id !=-1){
      //console.log(cartgory_Id);
      categoryId = cartgory_Id;
       url = url + "&categoryId=" + cartgory_Id;
    }
    $.ajax({
        url : url,
        type:"GET",
        success:function(responseResult){
          //console.log(responseResult);
          if(responseResult.data.length==0){
            alert("没有此书");
            return;
          }
          showView(responseResult);
        },
    });
  });

//定义分类功能
$ul = {
  category_find_goods: function (category_id) {
    $.ajax({
      url: "goods/pages?categoryId=" + category_id + "&pageNum=1&pageSize=12",
      type: "GET",
      success: function (result) {
        categoryId = category_id;
        ol_li(categoryId);
        showView(result);
      }
    });
  }
}
//获取分类菜单数据
let cartgory_li = 0;
 

//页面加载完后再调用分类功能
  $.ajax({
    type: "GET",
    url: "category/all",
    async: true,
    success: function (response) {
      showCategory(response);
    }
  });
  
  start();
  // 发送商品请求
  function start() {
    let uri = window.location.href;
    let uri_array = uri.split("=");
    let html_obj = JSON.parse(window.sessionStorage.getItem("html_obj"));
   
    if (uri_array.length > 1) {
      categoryId = uri_array[1];
      if(html_obj != null){
        serach_text_over = html_obj.bookname;
        categoryId = html_obj.cartgoryid;
      }else{
        categoryId = uri_array[1];
      }
      
      monitorResponse(1);
      ol_li(categoryId);
    } else {
      firstPage();
    }
  }
  //首页
  function firstPage() {
    //调用监听方法
    let pageNum = 1;
    monitorResponse(pageNum);
  }
  
  //为当前标签设置状态
  function ol_li(categoryId){
    showCategory(cartgory_li);
  }

  //设置当前的菜单分类
  function showCategory(response) {
    cartgory_li = response;
    $("#breadcrumb_ol").empty();
    $("#breadcrumb_ol").append($("<li class='breadcrumb-item'><a href='book.html'>Home</a></li>"));
    let dropdown_item_list = $(".dropdown-item");
   
    $("#down_list").empty();

    for (let i = 0; i < response.length; i++) {
      let category = response[i];
      //菜单分类
      let li = $("<li></li>").prop("class","breadcrumb-item").prop("id","id" + (i+1)).prop("value",category.id) ;
      let a = $("<a></a>").attr("onclick","$ul.category_find_goods(" + category.id + ")").text(category.name).prop("href","#").prop("id","a_id"+(i+1));
      li.append(a);
      $("#breadcrumb_ol").append(li);
      
      //下拉按钮的值
      let down_li = $("<li></li>").prop("class","dropdown-item").text(category.name);
      down_li.prop("value",category.id);
      down_li.click(function(){
          $("#btn_text").text(down_li.text());
          $("#btn_text").val(down_li.val());
      });
      //加入到下拉按钮
      $("#down_list").append(down_li);
      // <a class="dropdown-item" href="#">Another action</a>
    }
    $("#id"+categoryId).attr("class","breadcrumb-item active");
    $("#a_id"+categoryId).removeAttr("href");
  }
  //打开时 调用一次获取首页的数据
  function monitorResponse(pageNum) {
    let url = "/goods/pages?pageNum=" + pageNum + "&pageSize=12";
    if (categoryId != -1) {
      url = url + "&categoryId=" + categoryId;
      ol_li(categoryId);
    }
    console.log(serach_text_over);
    if(serach_text_over != "" && serach_text_over!=null ){
      url = url + "&bookname=" + serach_text_over;
      //console.log(url);
    }
    $.ajax({
      url: url,
      type: "GET",
      success: function (responseResult) {
        showView(responseResult);
        console.log(responseResult);
      }
    });
  }
  //显示商品信息
  function showView(responseResult) {
   
    let good_list1 = document.getElementById("goods_list");
    //找到所有children
    
    let good_list_childrens = good_list1.children;
    //删除之前的所有节点

    for (let i = good_list_childrens.length - 1; i >= 0; i--) {
      good_list1.removeChild(good_list_childrens[i]);
    }

    let good_list = $("#goods_list");
    for (let i = 0; i < responseResult.data.length; i++) {
      let goods = responseResult.data[i];
      //创建div
      let div = $("<div></div>").prop("class", "col-sm-3 bd-detail-box").attr("style", "height=260px;");
      div.prop("title",goods.name);
    
      //图片跳转
      //跳转页面 将当前商品的id作为参数传递给另一个页面
      //rel="opener"
      //rel="opener noopener nofollow ugc">
      let a = $("<a></a>").prop("href", "bookMessage.html?goodsid=" + goods.id);
    
      let img = $("<img>").prop("src", "/" + goods.image).prop("class", "img-rounded");
      a.append(img);

      //商品名字
      let p_book_name = $("<p></p>").text(goods.name).prop("class", "goods_name1");

      //商品惊喜价
      let p_surprise_price = $("<p></p>").text("惊喜价:" + goods.salesprice);

      //商品市场价
      let mark_price = $("<p></p>").text("市场价：￥");
      let mark_price_span = $("<span></span>").text(goods.marketprice).prop("class", "market_price");
      mark_price.append(mark_price_span);


      div.append(a);
      div.append(p_book_name);
      div.append(p_surprise_price);
      div.append(mark_price);

      good_list.append(div);
    }
    $("#current_page").text(responseResult.currentPage);
    $("#total_page").text(responseResult.totalPage);

    $("#goPage").val($("#current_page").text());
  }


  //上一页
  $("#prevPage").click(function () {
    if(isDisabled("#prevPage")){
      return;
    }
    $("#nextPage").removeClass("disabled");
    let current_page = parseInt(document.getElementById("current_page").innerText);
    if (current_page == 1) {
      alert("已经是最前面了");
      $("#prevPage").addClass("disabled");
      return;
    }
    current_page--;
    //调用监听方法
    monitorResponse(current_page);
  });


  //下一页
  $("#nextPage").click(function () {
    if(isDisabled("#nextPage")){
      return;
    }
    $("#prevPage").removeClass("disabled");
    let current_page = parseInt(document.getElementById("current_page").innerText);
    let total_page = parseInt(document.getElementById("total_page").innerText);
    if (current_page == total_page) {
      $("#nextPage").addClass("disabled");
      alert("已经是最末尾了");
      return;
    }
    //2.计算出下一页
    current_page++;
    //调用监听方法
    monitorResponse(current_page);
  });

  //尾页
  $("#endPage").click(function () {
    $("#prevPage").removeClass("disabled");
    let total_page = parseInt(document.getElementById("total_page").innerText);
    monitorResponse(total_page);
    
  });
  //首页
  $("#firstPage").click(function () {
    $("#nextPage").removeClass("disabled");
    firstPage();
  });
  
  //跳转
  $("#pageJump").click(function () {
    let goPage = $("#goPage").val();
    if (goPage == null) {
      alert("页数不能为空");
      return;
    }
    

    let total_page = parseInt($("#total_page").text());
    //大于最大页数
    if (goPage > total_page) {
      goPage = total_page;
      $("#prevPage").removeClass("disabled");
    }

    //小于0页面
    if (goPage <= 1) {
      goPage = 1;
      $("#nextPage").removeClass("disabled");
    }

    //大于1 并且小于最大页数
    if(goPage > 1 && goPage < total_page){
      $("#nextPage").removeClass("disabled");

      $("#prevPage").removeClass("disabled");
    }

    $("#goPage").val("");
    monitorResponse(goPage);
  });
  
  //获得热卖商品
 
  hot_shop();
  function hot_shop(){
  
    $.ajax({
      type:"POST",
      url:"/woniumall2/goods/hotShop",
      data:{
        "nums":5,
      },
      success: function (responseResult) {
        //查询成功
        console.log(responseResult);
        if(responseResult.status == "SELECT_SUCCESS"){
          $("#images_ol").empty();
          $("#images_div_list").empty();
          for(let i=0;i < responseResult.data.length; i++){
            let hot_goods = responseResult.data[i];
            //console.log(hot_goods);
           
            let li = $("<li></li>").attr("data-target","#images_div").attr("data-slide-to",i);
            if(i==0){
              li.prop("class","active");
            }
            //将li加入到ol
            $("#images_ol").append(li);

            //新建a和img标签
            let a =$("<a></a>");
            a.attr("href","/woniumall2/bookMessage.html?goodsid=" + hot_goods.id);
            let img=$("<img>").prop("class","d-block w-100 img").prop("alt","...").prop("src", hot_goods.image);

          
            a.append(img);
            //创建div包住图片
         
            
            
             //创建文字的div
             let text_div =$("<div></div>").prop("class","carousel-caption d-none d-md-block");
          
             let h5 = $("<h5></h5>").text(hot_goods.name.substring(0,6)).prop("class","p5");
             let p =$("<p></p>").text(hot_goods.description.substring(0,15)).prop("class","p_style");
             
             text_div.append(h5);
             text_div.append(p);

            //创建外面的div
            let div = $("<div></div>").css("height","300px");
            if(i==0){
              div.prop("class","carousel-item active");
            }else{
              div.prop("class","carousel-item");
            }
         
            div.append(a);
            div.append(text_div);
                   

            $("#images_div_list").append(div);

          }
        }else{
          return;
        }
      },
    });


    
  }
});
